package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {
    @Autowired
    private TbClueMapper tbClueMapper;
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    /**
     * 线索跟进要修改线索表,和向线索跟进表中添加数据
     *
     * @param tbClueTrackRecord
     * @return
     */
    @Override
    @Transactional
    public void add(ClueTrackRecordVo tbClueTrackRecord) {
        //修改线索表
        TbClue tbClue = new TbClue();
        //将tbClueTrackRecord 中的属性赋值给tbClue 对应属性
        BeanUtils.copyProperties(tbClueTrackRecord, tbClue);
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClue.setCreateTime(new Date());
        tbClue.setCreateBy(SecurityUtils.getUsername());
        //修改线索的状态为2 ,跟进中
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        //
        tbClueMapper.updateTbClue(tbClue);

        //向线索追踪表中添加数据
        TbClueTrackRecord tbClueTrackRecord1 = new TbClueTrackRecord();
        BeanUtils.copyProperties(tbClueTrackRecord, tbClueTrackRecord1);
        tbClueTrackRecord1.setCreateBy(SecurityUtils.getUsername());
        tbClueTrackRecord1.setCreateTime(new Date());
        tbClueTrackRecordMapper.add(tbClueTrackRecord1);
    }

    /**
     * 查询线索跟进记录列表
     */
    @Override
    public TableDataInfo list(Long clueId) {

        TableDataInfo tableDataInfo = new TableDataInfo();

        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.list(clueId);
        tableDataInfo.setTotal(list.size());
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
}
